package com.sfh.cpc.com.ui;

import android.Manifest;
import android.content.Intent;
import android.support.annotation.NonNull;

import com.sfh.cpc.com.ImageSelector;
import com.sfh.lib.mvvm.service.BaseViewModel;
import com.sfh.lib.ui.AbstractLifecycleActivity;

import java.util.List;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * 功能描述:权限处理
 *
 * @author SunFeihu 孙飞虎
 * @date 2018/5/10
 */
public abstract class SelectTakeImageActivity<VM extends BaseViewModel> extends BasePermissionsActivity<VM> implements EasyPermissions.PermissionCallbacks {


    public  int selectSize = 1;
    public abstract void onSelectImageOK(List<String> select);

    @Override
    public String[] getPermissions() {
        return new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
    }

    @Override
    public void onPermissionsSuccess() {
        //权限申请已经成功
        ImageSelector.create().showCamera(true).count(selectSize).start(this, IMAGE_CODE);
    }


    private static final int IMAGE_CODE = 1000;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (IMAGE_CODE == requestCode) {
            List<String> select = ImageSelector.onActivityResult(resultCode, data);
            if (select == null || select.isEmpty()) {
                return;
            }
            onSelectImageOK(select);
        }
    }
}
